package com.hnzq.cloud.sendassembly.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hnzq.cloud.IO.AddSendOrderIO;
import com.hnzq.cloud.IO.DelOrderForSendIO;
import com.hnzq.cloud.IO.EditSendOrderIO;
import com.hnzq.cloud.IO.entity.FreSend;
import com.hnzq.cloud.IO.entity.FreSendLog;
import com.hnzq.cloud.IO.entity.FreSendOrder;
import com.hnzq.cloud.api.FreSendOrderApi;
import com.hnzq.cloud.model.Result;
import com.hnzq.cloud.sendassembly.entity.FreSendOrderE;
import com.hnzq.cloud.sendassembly.exception.FreException;
import com.hnzq.cloud.sendassembly.service.FreSendLogService;
import com.hnzq.cloud.sendassembly.service.FreSendOrderService;
import com.hnzq.cloud.sendassembly.service.FreSendService;
import com.hnzq.cloud.util.ComUtil;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tiangle
 * @since 2022-02-07
 */
@RestController
@RequestMapping("freSend")
public class FreSendController implements FreSendOrderApi {
    @Autowired
    private FreSendService freSendService;
    @Autowired
    private FreSendLogService freSendLogService;
    @Autowired
    private FreSendOrderService freSendOrderService;
    /**
    * @作者: tiangle
    * @描述：根据sendid查询send，不包括sendorder
    * @时间：2022/2/10 16:12
    * @参数类型:[id]
    * @返回：com.hnzq.cloud.util.Result
    **/
    @Override
    public Result<FreSend> getById(Long id) {

        FreSend byId = freSendService.getById(id);
        System.out.println("getById:11=="+byId);
        if(byId==null||byId.getIsDelete()==1)
            return Result.newInstance().setCode(500).setMessage("不存在该订单");
        return Result.newInstance().setData(byId).setCode(200);
    }

    @Override
    public Result<List<Integer>> getOrders(Long id) {
        QueryWrapper<FreSendOrderE> freSendOrderQueryWrapper = new QueryWrapper<>();
        freSendOrderQueryWrapper.eq("send_id",id);
        List<Integer> collect = freSendOrderService.list(freSendOrderQueryWrapper).stream().map(a -> a.getOrderId()).collect(Collectors.toList());

        return Result.newInstance().setData(collect).setCode(200).setMessage("查询成功");
    }
    /**
    * @作者: tiangle
    * @描述：根据司机id查询配送单
    * @时间：2022/2/22 17:22
    * @参数类型:[id]
    * @返回：com.hnzq.cloud.model.Result<com.hnzq.cloud.IO.entity.FreSend>
    **/
    @Override
    public Result<FreSend> getByDriver(Integer id) throws FreException {
        QueryWrapper<FreSend> freSendOrderQueryWrapper = new QueryWrapper<>();
        freSendOrderQueryWrapper.eq("driver_id",id).eq("send_status",0);
        FreSend one = freSendService.getOne(freSendOrderQueryWrapper);
        if (one==null){
            throw new FreException("没有未配送的订单",500);
        }
        return Result.newInstance().setCode(200).setData(one);
    }

    @Override
    public Result<FreSend> getSendByOrder(Integer id) {
        Long sendByOrder = freSendOrderService.getSendByOrder(id);
        FreSend byId = freSendService.getById(sendByOrder);
        return Result.newInstance().setCode(200).setData(byId);
    }

    @Override
    public Result<List<FreSend>> getAll() {
        List<FreSend> list = freSendService.list();
        return Result.newInstance().setCode(200).setData(list);
    }

    /**
    * @作者: tiangle
    * @描述：添加一条send，包括了orderid
    * @时间：2022/2/10 16:13
    * @参数类型:[addSend]
    * @返回：com.hnzq.cloud.util.Result
    **/
    @GlobalTransactional
    @Override
    public Result<Long> addSendOrder(AddSendOrderIO addSend) throws FreException {
        System.out.println("景来了");
        addSend = freSendService.addSend(addSend);
        Long sendId = addSend.getSendId();
        System.out.println("addSendOrderController:"+addSend.toString());
        boolean b = freSendOrderService.addToSend(addSend);
        System.out.println(b);
        if (b) {
            System.out.println("if里面:"+sendId);
            return Result.newInstance().setData(sendId).setCode(200).setMessage("创建订单成功");

        }
        throw new FreException("创建订单失败",500);

    }
    /**
    * @作者: tiangle
    * @描述：
    * @时间：2022/2/10 17:58
    * @参数类型:[editSend]
    * @返回：com.hnzq.cloud.util.Result
    **/
    @GlobalTransactional
    @Override
    public Result editSendOrder(EditSendOrderIO editSend) throws FreException {
        FreSend freSend = ComUtil.copyProperties(editSend, FreSend.class);
        FreSend byId = freSendService.getById(editSend.getSendId());
        if (byId==null||byId.getIsDelete()==1){
            throw new FreException("修改失败，该send不存在",500);

        }
        QueryWrapper<FreSend> freSendQueryWrapper = new QueryWrapper<>();
        freSendQueryWrapper.eq("send_id",freSend.getSendId());
        freSendService.update(freSend,freSendQueryWrapper);
        return Result.newInstance().setCode(200).setMessage("修改成功");
    }
    @GlobalTransactional
    @Override
    public Result editStatus(EditSendOrderIO editSend) throws FreException {
        System.out.println(editSend);
        if (freSendService.editSend(editSend)) {
            FreSendLog freSendLog = new FreSendLog();
            freSendLog.setLogTime(ComUtil.nowDateTime());
            freSendLog.setSendId(editSend.getSendId());
            freSendLog.setSendStatus(editSend.getSendStatus());
            freSendLog.setSendDesc(editSend.getDesc());
            if (freSendLogService.addLog(freSendLog)) {
                return Result.newInstance().setCode(200).setMessage("状态修改成功");
            }
        }
        return Result.newInstance().setCode(500).setMessage("修改失败");
    }

    /**
    * @作者: tiangle
    * @描述：添加order到send里面
    * @时间：2022/2/11 9:29
    * @参数类型:[addSendOrderIO]
    * @返回：com.hnzq.cloud.util.Result
    **/
    @GlobalTransactional
    @Override
    public Result addToSend(AddSendOrderIO addSendOrderIO) throws FreException {
        FreSend byId = freSendService.getById(addSendOrderIO.getSendId());
        if (byId==null||byId.getIsDelete()==1){
            throw new FreException("添加失败，该send不存在",500);

        }
        if (freSendOrderService.addToSend(addSendOrderIO)) {
            FreSend byId2 = freSendService.getById(addSendOrderIO.getSendId());
            byId.setSendWeight(byId2.getSendWeight().add(addSendOrderIO.getSendWeight()));
            byId.setSendPrice(byId2.getSendPrice().add(addSendOrderIO.getSendPrice()));
            EditSendOrderIO  editSendOrderIO = ComUtil.copyProperties(byId2, EditSendOrderIO.class);
            freSendService.editSend(editSendOrderIO);
            return Result.newInstance().setMessage("添加成功").setCode(200);
        }
        throw new FreException("添加失败",500);

    }
    //删除send里面的order
    @GlobalTransactional
    @Override
    public Result deleteForSend(DelOrderForSendIO delOrderForSendIO) throws FreException {
        FreSend byId = freSendService.getById(delOrderForSendIO.getSendId());
        if (byId==null||byId.getIsDelete()==1){
            throw new FreException("删除失败，该send不存在",500);

        }
        QueryWrapper<FreSendOrderE> wrapper = new QueryWrapper<>();
        wrapper.eq("send_id",delOrderForSendIO.getSendId()).in("order_id",delOrderForSendIO.getOrderIds());
        freSendOrderService.remove(wrapper);
        return Result.newInstance().setCode(200);
    }
    @GlobalTransactional
    @Override
    public Result deleteSend(Long id) throws FreException {
        FreSend byId = freSendService.getById(id);
        if (byId==null)
            throw new FreException("未找到该send",500);

        QueryWrapper<FreSend> freSendQueryWrapper = new QueryWrapper<>();
        freSendQueryWrapper.eq("send_id",byId.getSendId());

        if (freSendService.remove(freSendQueryWrapper)) {
            return Result.newInstance().setCode(200).setMessage("删除成功");
        }

        throw new FreException("删除失败",500);
    }
}

